package com.init.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.init.model.dto.CategoriesDto;
import com.init.model.entity.Items;
import com.init.model.entity.Categories;
import com.init.service.ItemsService;
import com.init.service.CategoriesService;
import com.init.mapper.CategoriesMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
* @author Atanycosts
* @description 针对表【categories(存储大标题信息)】的数据库操作Service实现
* @createDate 2025-01-05 16:11:38
*/
@Service
public class CategoriesServiceImpl extends ServiceImpl<CategoriesMapper, Categories>
    implements CategoriesService{

    @Resource
    @Lazy
    private CategoriesService categoriesService;

    @Resource
    private CategoriesMapper categoriesMapper;

    @Resource
    private ItemsService itemsService;

    /**
     * 分页查询
     * 或者通过 title 进行模糊查询
     * @param page
     * @param pageSize
     * @param title
     * @return
     */
    @Override
    public Page queryPage(int page, int pageSize, String title) {
        // 构造分页构造器
        Page pageInfo = new Page(page, pageSize);

        // 构造条件构造器
        LambdaQueryWrapper<Categories> queryWrapper = new LambdaQueryWrapper();

        // 添加过滤条件,模糊查询

        queryWrapper.like(StringUtils.isNotEmpty(title), Categories::getTitle, title);

        // 添加排序条件
        queryWrapper.orderByDesc(Categories::getCategoryId);

        // 执行查询
        categoriesService.page(pageInfo, queryWrapper);
        return pageInfo;
    }

    /**
     * 根据ID查询详细数据   ---封装
     *
     * @param categoriesId
     */
    @Override
    public CategoriesDto queryDetail(String categoriesId) {
        CategoriesDto categoriesDto = new CategoriesDto();

        // 查询大标题信息
        Categories categories = categoriesMapper.selectById(categoriesId);

        if (categories != null) {
            categoriesDto.setAdminId(categories.getAdminId());
            categoriesDto.setCategoryId(categories.getCategoryId());
            categoriesDto.setTitle(categories.getTitle());
            categoriesDto.setCreateTime(categories.getCreateTime());

            // 查询大标题的评定内容
            List<Items> items =
                    categoriesMapper.selectAssessmentItemsByCategoryId(categories.getCategoryId());
            categoriesDto.setItems(items);
        }

        return categoriesDto;
    }

}




